package com.robaone.gwt.hierarcialdb.client.ui.admin;

import java.util.HashMap;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.robaone.gwt.hierarcialdb.client.Hierarcialdbadmin;
import com.robaone.gwt.hierarcialdb.client.data.DataConstants;
import com.robaone.gwt.hierarcialdb.client.state.AdminState;

public class AdminUi extends Composite {

	private static AdminUiUiBinder uiBinder = GWT.create(AdminUiUiBinder.class);

	interface AdminUiUiBinder extends UiBinder<Widget, AdminUi> {
	}

	public AdminUi() {
		initWidget(uiBinder.createAndBindUi(this));
		try {
			setFolders("/");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@UiField FlowPanel folders;
	@UiField FlowPanel foldergrid;

	public void setFolders(String path) throws Exception {
		this.setFolderPath(path);
		try{
			updateFolders(path);
		}catch(Exception e){}
	}

	private void setFolderPath(String path) {
		folders.clear();
		if(path.startsWith("/")){
			path = "ROOT"+path;
		}else{
			path = "ROOT/"+path;
		}
		String[] tokens = path.split("/");
		String folder_path = "";
		for(int i = 0; i < tokens.length;i++){
			if(i > 0) folders.add(new InlineLabel("/"));
			InlineLabel folder = new InlineLabel(tokens[i]);
			folder.getElement().setAttribute("style", "cursor:pointer");
			folders.add(folder);
			if(i > 0) folder_path += "/";
			folder_path += tokens[i];
			final String current_path = folder_path;
			folder.addClickHandler(new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					updateFolders(current_path);
				}

			});
		}

	}
	private void setFilePath(String path,final String id) {
		folders.clear();
		if(path.startsWith("/")){
			path = "ROOT"+path;
		}else{
			path = "ROOT/"+path;
		}
		String[] tokens = path.split("/");
		String folder_path = "";
		for(int i = 0; i < tokens.length;i++){
			if(i > 0) folders.add(new InlineLabel("/"));
			InlineLabel folder = new InlineLabel(tokens[i]);
			folder.getElement().setAttribute("style", "cursor:pointer");
			folders.add(folder);
			if(i > 0) folder_path += "/";
			folder_path += tokens[i];
			final String current_path = folder_path;
			if(i < tokens.length-1){
			folder.addClickHandler(new ClickHandler(){

				@Override
				public void onClick(ClickEvent event) {
					updateFolders(current_path);
				}

			});
			}else{
				folder.addClickHandler(new ClickHandler(){

					@Override
					public void onClick(ClickEvent event) {
						AdminState.showFileContents(id);
					}
					
				});
			}
		}
	}
	protected void updateFolders(String current_path) {
		Hierarcialdbadmin.hdbService.getFolderContentsbyPath(current_path,new AsyncCallback<Vector<HashMap<String,String>>>(){

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Vector<HashMap<String,String>> result) {
				try{
					populateContent(result);
				}catch(Exception e){
					onFailure(new Throwable(e));
				}

			}

		});
		Label l = new Label(current_path);
	}

	public void showFolderbyId(String id) {
		Hierarcialdbadmin.hdbService.getFolderContentsbyId(id,new AsyncCallback<Vector<HashMap<String,String>>>(){

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(Vector<HashMap<String,String>> result) {
				try{
					populateContent(result);
				}catch(Exception e){
					onFailure(new Throwable(e));
				}
			}

		});
	}

	protected void populateContent(Vector<HashMap<String, String>> result) throws Exception {
		foldergrid.clear();
		for(int i = 0; i < result.size();i++){
			String filename = (String)result.get(i).get("filename");
			String type = (String)result.get(i).get("type");
			String id = (String)result.get(i).get("id");
			String path = (String)result.get(i).get("path");
			if(path == null){
				if(type.equalsIgnoreCase("0")){
					FolderUi folder = new FolderUi(filename,id);
					foldergrid.add(folder);
				}else{
					FileUi file = new FileUi(filename,type,id);
					foldergrid.add(file);
				}
			}else{
				/*
				 * Set the folder path
				 */
				this.setFolderPath(path);
			}
		}
	}

	public void showFileContents(String id) {
		Hierarcialdbadmin.hdbService.getFileContentsbyId(id,new AsyncCallback<Vector<HashMap<String,String>>>(){

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				
			}

			@Override
			public void onSuccess(Vector<HashMap<String, String>> result) {
				try{
					populateFileContent(result);
				}catch(Exception e){
					onFailure(new Throwable(e));
				}
			}
		
		});
	}

	protected void populateFileContent(Vector<HashMap<String, String>> result) throws Exception {
		foldergrid.clear();
		FileViewerUi viewer = new FileViewerUi(result.get(0));
		foldergrid.add(viewer);
	}
}
